<?php

namespace app\admin\controller;
use think\exception\ValidateException;
use app\admin\model\CharitableClass as CharitableClassModel;
use app\common\model\CharitableArticle as CharitableArticleModel;

class Charitable extends Admin
{
    /**
     * 慈善分类列表
     */
    function index(){

        if (!$this->request->isPost()){
            return view('index');
        }else{
            $limit  = $this->request->post('limit', 20, 'intval');
            $page = $this->request->post('page', 1, 'intval');
            $order  = $this->request->post('order', '', 'serach_in');	//排序字段
            $sort  = $this->request->post('sort', '', 'serach_in');		//排序方式

            $orderby = ($sort && $order) ? $sort.' '.$order : 'id desc';
            $res = CharitableClassModel::order($orderby)->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();

            $data['status'] = 200;
            $data['data'] = $res;
            return json($data);
        }
    }

    /**
     * 获取所有分类列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function class_list(){
        $res = CharitableClassModel::select()->toArray();
        $data['status'] = 200;
        $data['data'] = $res;
        return json($data);
    }

    /**
     * 分类添加
     * @return \think\response\Json
     */
    function add(){
        $postField = 'title,e_title,t_title,pic,e_pic,t_pic';
        $data = $this->request->only(explode(',',$postField),'post',null);
        $data['add_time'] = date('Y-m-d H:i:s',time());

        $res = CharitableClassModel::insertGetId($data);

        if(empty($res)) return json(['status'=>200,'data'=>$res,'msg'=>'添加失败']);
        return json(['status'=>200,'data'=>$res,'msg'=>'添加成功']);
    }

    /**
     * 分类修改
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getEdit(){
        $id =  $this->request->post('id', '', 'serach_in');
        if(!$id) throw new ValidateException ('参数错误');
        $res = CharitableClassModel::find($id);
        return json(['status'=>200,'data'=>$res]);
    }

    /**
     * 分类修改
     * @return \think\response\Json
     */
    function edit(){
        $postField = 'id,title,e_title,t_title,pic,e_pic,t_pic';
        $data = $this->request->only(explode(',',$postField),'post',null);
        CharitableClassModel::where('id',$data['id'])->update($data);
        return json(['status'=>200,'msg'=>'修改成功']);
    }

    /**
     * 分类删除
     * @return \think\response\Json
     */
    function delete(){
        $idx =  $this->request->post('id', '', 'serach_in');
        if(!$idx) throw new ValidateException ('参数错误');
        CharitableClassModel::destroy(['id'=>explode(',',$idx)],true);
        return json(['status'=>200,'msg'=>'操作成功']);
    }

    /**
     * 获取某个分类文章列表
     */
    function list(){
        if (!$this->request->isPost()){

            $class = CharitableClassModel::select()->toArray();
            return view('list',['charitable_class'=>json_encode($class)]);

        }else{
            $limit  = $this->request->post('limit', 20, 'intval');
            $page = $this->request->post('page', 1, 'intval');

            $where = [];
            $where['title'] = ['like',$this->request->post('title', '', 'serach_in')];
            $where['cid'] = $this->request->post('cid', '', 'intval');
            $where['language'] = $this->request->post('language', '', 'intval');
            $orderby = 'id desc';
            $res = CharitableArticleModel::where(formatWhere($where))->order($orderby)->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();

            foreach ($res['data'] as &$v){
                //查询文章分类标题
                $class = CharitableClassModel::getDetails($v['cid']);
                $v['cname'] = !empty($class) ? $class['title'] : '';

            }

            $data['status'] = 200;
            $data['data'] = $res;
            return json($data);
        }
    }

    /**
     * 添加文章
     * @return \think\response\Json\
     */
    function article_add(){
        $postField = 'language,title,author,author_logo,pic,cid,content';
        $data = $this->request->only(explode(',',$postField),'post',null);
        $data['content'] = html_entity_decode($data['content']);    //实体字符转义

        $data['add_time'] = date('Y-m-d H:i:s',time());
        $data['update_time'] = date('Y-m-d H:i:s',time());

        $res = CharitableArticleModel::insertGetId($data);
        if(empty($res)) return json(['status'=>200,'data'=>$res,'msg'=>'添加失败']);
        return json(['status'=>200,'data'=>$res,'msg'=>'添加成功']);
    }

    /**
     * 获取文章详情
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function article_getEidt(){
        $postField = 'id';
        $data = $this->request->only(explode(',',$postField),'post',null);
        $res = CharitableArticleModel::getDetails($data['id']);
        return json(['status'=>200,'data'=>$res]);
    }

    /**
     * 文章修改
     * @return \think\response\Json
     */
    function article_edit(){
        $postField = 'id,language,title,author,author_logo,pic,cid,content';
        $data = $this->request->only(explode(',',$postField),'post',null);
        $data['content'] = html_entity_decode($data['content']);    //实体字符转义
        //修改文章
        $res = CharitableArticleModel::edit($data['id'],$data);
        if(empty($res)) return json(['status'=>200,'data'=>'','msg'=>'修改失败']);
        return json(['status'=>200,'data'=>$res,'msg'=>'修改成功']);
    }

    /**
     * 文章删除
     * @return \think\response\Json
     */
    function article_del(){
        $idx =  $this->request->post('id', '', 'serach_in');
        if(!$idx) throw new ValidateException ('参数错误');
        CharitableArticleModel::destroy(['id'=>explode(',',$idx)],true);
        return json(['status'=>200,'msg'=>'操作成功']);

    }

}